#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int dx[4] = { 1 , -1,  0, 0 };
int dy[4] = { 0 , 0 , -1 , 1 };
int n;

int maxDistance(vector<vector<int>>& grid) {
    n = grid.size();
    int step = -1;
    vector<vector<int>> ret(n, vector<int>(n, -1));
    queue<pair<int, int>> q;

    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            if (grid[i][j] == 1)
            {
                ret[i][j] = 0;
                q.push({ i,j });
            }

    while (q.size())
    {
        auto [a, b] = q.front();
        q.pop();

        for (int i = 0; i < 4; ++i)
        {
            int x = a + dx[i], y = b + dy[i];
            if (x >= 0 && x < n && y >= 0 && y < n && ret[x][y] == -1)
            {
                q.push({ x,y });
                ret[x][y] = ret[a][b] + 1;
                step = max(step, ret[x][y]);
            }
        }
    }

    return step;
}

int main()
{

	return 0;
}